package ug.co.icom.kyazze.healthticket.mongoHQ;

import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import ug.co.icom.kyazze.healthticket.data.QueryBuilder;
import ug.co.icom.kyazze.healthticket.data.RequestConnection;
import android.os.AsyncTask;

public class MongoHQRequestConnection extends
		AsyncTask<RequestConnection, Void, Boolean> {

	@Override
	protected Boolean doInBackground(RequestConnection... arg0) {
		try {
			RequestConnection rc = new RequestConnection();
			rc = arg0[0];
			QueryBuilder qb = new QueryBuilder();
			
			URL url = new URL(qb.buildPersonalDetailsUpdate(rc.ID));
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.setRequestMethod("PUT");
			connection.setDoOutput(true);
			connection.setRequestProperty("Content-Type",
					"application/json");
			connection.setRequestProperty("Accept", "application/json");
			
			OutputStreamWriter osw = new OutputStreamWriter(
					connection.getOutputStream());
			
			osw.write(qb.getRequestConnDetails(rc.user, rc.permissions));
			osw.flush();
			osw.close();
			if(connection.getResponseCode() <205)
			{
				return true;
			}
			else
			{
				return false;
			}
			
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	protected void onPostExecute(Boolean result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
	}
}
